# # 列表

# print(list('hello'))
# print([1, 'hello', True, [1,2,3]])
# ls1 = [1, 2, 3, 5, 3]
# print(len(ls1))
# print(ls1[1])
# print(ls1[-1]) # ls1[len(ls1)-1] 的简便写法（负号可以理解为倒着数）
# # 判断元素存在及查找
# print(1 in ls1) # in判断元素是否在列表中，返回布尔值
# print(2 not in ls1) # not为逻辑取反
# # 索引（用于查找元素位置）
# x = ls1.index(5) # 若数据不存在，会报错（不属于语法错误）
# print(x)
# # 计数
# count = ls1.count(3)
# print(count)
# # 增加
# ls1.append(6) #追加（尾插）
# ls1.insert(3, 4) # 插入（此处表示第4个位置插入4，插入位置后的元素往后挪）
# ls1.insert(100, 'hello')
# print(ls1)
# # 删除
# a = ls1.pop(1) # 析出（将第2个位置的元素取出并删除，若参数为空则删除后面的一个元素）
# ls1.remove(3) # 移除（删除第一次出现的3）
# del ls1[0] #删除命令（按索引删除）
# # ls1.clear() # 清空（清空列表元素，得到空列表）
# print(a)
# print(ls1)
# # 排序
# ls2 = [3, 1, 2, 0, 4]
# ls2.reverse() #倒转
# print(ls2)
# ls2.sort(reverse= False) #升序
# print(ls2)
# ls2.sort(reverse= True) #降序
# print(ls2)
# # 复制
# ls = [0, 1, 2, 3, 4, 5]
# lt1 = ls #此处赋值相当于重命名，没有复制
# lt1.append(6)
# print(ls, lt1)
# lt2 = ls.copy()
# lt2[0] = 1
# print(ls, lt2)
# Ls = [0, 1, 2, 3, [1, 2, 3]] #存在嵌套结构
# import copy
# lt4 = copy.deepcopy(Ls) # 深复制
# lt4[4][0] = 0
# print(Ls, lt4)
# lt3 = Ls.copy() #浅复制
# lt3[4][0] = 0
# print(Ls, lt3)
# # 遍历列表元素
# i =0
# while i < len(ls):
#     i += 1
# print(ls) # 不改变列表
# for i in ls:
#     i += 10 # 不改变列表
# print(ls)
# for i in range(0, len(ls)): # 刚好从ls[0]取到ls[len(ls)-1],即全部元素
#     ls[i] += 10 # 改变了列表
# print(ls)
# # 切片（得到子列表）
# # 切片操作不涉及数据拷贝，很高效
# ls3 = [0, 1, 2, 3, 4, 5]
# print(ls3[1:4]) # 取[1,4)
# print(ls3[1:]) # 省略后边界
# print(ls3[:4]) # 省略前边界
# print(ls3[:]) # 打印本身
# print(ls3[:-1]) # 取不到倒数第1个
# print(1, 100) # 切片操作范围允许超出列表长度
# print(ls3[::2]) # 第二个冒号后的参数是步长
# print(ls3[::-1]) # 步长为负数表示倒着取
# print(ls3[::-2])
# print(ls3[1:1]) # 空列表
# # 合并（拼接）
# m = [1, 2, 3, 4]
# n = [5, 6, 7, 8]
# u = m + n
# print(u)
# m.extend(n)
# print(m)
# m += n # 涉及数据的构造与释放，比extend更低效
# print(m)
# m[1:1] = n
# print(m)
# 列表推导式
# v = [i for i in m[::-1]]
# s = [i for i in range(0,len(m)+1)]
# t = [i for i in m if i % 2 == 0]
# print(v, s, t)
# 二维列表
# List = [[1,2,3], [4,5,6], [7,8,9]]
# List[1][2] = 0
# print(List)
# 实例



# 元组
# 列表中的元素可以修改，元组不可以（即只可以只读）
# print((1, 'hello',True, []))
# a = (1, 2, 3, 4, 5) # tuple()也能表示元组
# print(a[1], a[-1])
# print(a[1:3])
# 多元赋值是按照元组的方式工作的
# def getPoint():
#     x = 1
#     y = 2
#     return x, y
# a, b = getPoint()
# print(type(getPoint()))

# 集合（set）
# 集合是无序结构
# set1 = set() #空集合
# set2 = {1, 2, 2, 3} #对于相同元素，后存储的元素会覆盖原有元素
# set3 = set("apple")
# print(set1)
# print(set2)
# print(set3)
# 增加元素
# set1 = {1, 2, 3, 4}
# set1.add(6)
# print(set1)
# set1.update([5, 6, 7]) # update()的参数是组合数据类型
# print(set1)
# # 移除元素
# set1.discard(2) # 要删除的元素不在集合中，不会报错
# print(set1)
# # 随机析出
# x = set1.pop() # 若为空集合，则产生KeyError异常
# print(x)
# print(set1)
# # 清空元素
# set1.clear()
# print(set1)
# 判断集合有无相同元素
# A = {1, 2, 3, 4}
# B = {3, 4, 5, 6}
# print(A.isdisjoint(B)) #无相同元素返回Ture
# # 集合运算
# # 求并集
# print(A|B)
# print(A.union(B))
# # 求交集
# print(A&B)
# print(A.intersection(B))
# # 求差集
# print(A-B) # A减去A和B共有元素
# print(A.difference(B))
# # 求对称差
# print(A^B) # A|B减去A和B共有元素
# print(A.symmetric_difference(B))



# # 字典(dictionary) # 用来储存键值对
# # 键值对是键(key)与值(value)的映射关系（一一对应）
# # 字典对key的类型有约束(元组，字符串等可作为键)，对value的类型无约束
# # 对字典的操作都是针对key进行的
# # key不能重复
# """
# 对于字典来说，使用in或者[]来获取value都很高效（字典的底层是哈希表）
# 对于列表来说，使用in比较低效（需要遍历列表），使用[]比较高效
# """
# dic = {'ID': 100,
#        "name": 'Mike',
#        "home": 'Ocean'
#     }
# print(dic)
# print("ID" in dic) # in只用来判定key是否存在
# print("Mike" in dic) # in不能用来判断value是否存在（此处返回False）
# print(dic['ID']) # 根据键得到值
# # 增加
# dic['score'] = 90
# # 删除
# del dic["name"] # del dic是删除整个字典
# # 修改
# dic['score'] = 100
# # 取出
# x = dic.get("ID")
# y = dic.get("house", None)
# print(x)
# print(y)
# # 析出(取出同时删除)
# a = dic.pop("home")
# b = dic.pop("house", None)
# c = dic.popitem() # 随机删除键值对
# print(a)
# print(b)
# print(c)
# print(dic)
# # 清空
# dic.clear()
# print(dic) # 返回空字典
# # 遍历
# my_dic = {"English":91, "math":92, "Computer":93}
# for k in my_dic.keys():
#     print(k) #遍历键
# for v in my_dic.values():
#     print(v) #遍历值
# for i in my_dic.items():
#     print(i)  # 遍历键值对
# for key, value in my_dic.items():
#     print(key,value)
# # 排序
# new_dic1 = sorted(my_dic) # 字符串比较的是ASCII码值
# new_dic2 = {i:j for j,i in my_dic.items()} #键值互换
# new_dic3 = sorted(new_dic2)
# print(new_dic1)
# print(new_dic2)
# print(new_dic3)
# # 合并
# dic1 = {"ID":200, "Student":"Jack", "Home":"Star"}
# dic2 = {"English":94, "math":95, "Computer":96}
# for k,v in dic2.items():
#     dic1[k] = v
# print(dic1)
# dic1.update(dic2)
# print(dic1)
# ls1 = list(dic1.items()) + list(dic2.items())
# dic3 = dict(ls1)
# print(dic3)
# dic4 = dict(dic1,**dic2)
# print(dic4)



# 实例 1
# # 统计字符串中每个单词的出现次数
# text = "I am a learner, I have a dream."
# for char in ',.':
#     text = text.replace(char, '')
# d = text.split( )
# count = {}
# for word in d:
#     count[word] = count.get(word,0) + 1
# print(count)

# 实例 2
# attr = ["敏捷", "气势", "内力", "筋骨"]
# tab = [ ["萧峰", 20, 18, 20, 19], ["杨过", 18, 19, 20, 17], ["郭靖", 19, 20, 17, 18] ]
# # attribute 属性 table 表格
#
# total = []
# for i in range(0, len(tab)):
#     Sum = 0
#     for j in range(1, len(tab[0])):
#         Sum += tab[i][j] #固定人物（i）让属性（j）变化
#     # 也可以直接用sum()函数
#     # Sum = sum(tab[i][1:])
#     total.append(Sum)
#     print("{}的总武力值：{}".format(tab[i][0], total[i]))
#
# Max = max(total)
# i = total.index(Max)
# print(f"{tab[i][0]}的总武力值最高")
#
# ave = []
# for j in range(1, len(attr) + 1):
#     s = 0
#     for i in range(0, len(tab)):
#         s += tab[i][j] # 固定属性（j）让人物（i）变化
#     ave.append(s / len(tab))
#     print("三位大侠{}的平均分：{}".format(attr[j-1], ave[j-1]))


# heroes = [{'大侠':"萧峰","敏捷":20, "气势":18, "内力":20, "筋骨":19},
#           {'大侠':"杨过","敏捷":18, "气势":19, "内力":20, "筋骨":17},
#           {'大侠':"郭靖","敏捷":19, "气势":20, "内力":17, "筋骨":18}]
#
# Total = []
# Sum = {}
# for hero in heroes:
#     total = 0
#     for k,v in hero.items():
#         if k != '大侠':
#             total += v
#             Sum[k] = Sum.get(k,0) + v
#     Total.append(total)
#     print("{}的总武力值：{}".format(hero['大侠'],total))
#
# Max = max(Total)
# i = Total.index(Max)
# print(f"{heroes[i]['大侠']}的总武力值最高")
#
# n = len(heroes)
# for k,v in Sum.items():
#     print("三位大侠{}的平均分：{}".format(k,Sum[k]/n))



# 实例 3
# ls = [{"性别":"男",'饮食':400,'文具':50,'衣服':300},
#       {"性别":"女",'饮食':230,'文具':60,'衣服':400},
#       {"性别":"女",'饮食':210,'文具':70,'衣服':350},
#       {"性别":"男",'饮食':300,'文具':60,'衣服':320}]
#
# Sum = {}
# for stu in ls:
#     for k,v in stu.items():
#         if k != '性别':
#             Sum[k] = Sum.get(k, 0) + v
# n = len(ls)
# for k,v in Sum.items():
#     print("{}的平均消费金额为{}元".format(k, v/n))
#
# Total = {}
# StuNum = {}
# for stu in ls:
#     s = 0
#     for k,v in stu.items():
#         if k != "性别":
#             s += v #累加总金额
#     x = stu['性别'] #判断性别
#     Total[x] = Total.get(x, 0) + s
#     StuNum[x] = StuNum.get(x, 0) + 1
# for sex in ["男", "女"]:
#     print("{}生平均消费金额为{}".format(sex, Total[sex] / StuNum[sex]))